//   分页列表    锁定    新增医院设置
import { 
  getHospitalSet, 
  getHospitalSetLockId, 
  postHospitalSet, 
  deleteHospitalSetById, 
  deleteBatchMove,
  putHospitalSet
 } from '@/api/hosp';
import { createSlice } from '@reduxjs/toolkit';
import { useSelector } from 'react-redux';


// 引入分页 加载中的actions
import { changeLoading, changePageInfo } from "@/store/slice/common";



const hospSlice = createSlice({
  name: 'hosp',
  initialState: {
    hospSetList: [],
    params:{}
  },
  reducers: {
    // 更新医院列表
    upHospSetList(state, { payload }) {
      state.hospSetList = payload
    },
    // 改变前台的状态
    changeStateById(state, { payload }) {
      const info: any = state.hospSetList.find((p: any) => p.id === payload)
      info.status = info.status === 1 ? 0 : 1
    },
    // ???搜索用到的
    changeParams(state, { payload }) {
      state.params = payload;
      console.log(payload); 
    },

  }
})

// 获取actions
export const { upHospSetList, changeStateById,changeParams } = hospSlice.actions


// 请求分页数据 接口 
export const getHospitalSetAsync = (page: number, limit: number) => (
  async (dispatch: any,getState:any) => {
    dispatch(changeLoading(true))
    const result: any = await getHospitalSet(page, limit, getState().hosp.params)
    dispatch(upHospSetList(result.data.records)) // 更新列表状态

    // 更新分页
    dispatch(changePageInfo({
      current: result.data.current,
      total: result.data.total,
      pageSize: result.data.size
    }))

    dispatch(changeLoading(false))

  }
)


// 锁定状态 用接口做 (发起多个ajax请求)
export const getHospitalSetLockIdAsync = (id: number, status: number, params: any = {}) => (
  async (dispatch: any, getState: any) => {
    await getHospitalSetLockId(id, status)
    const { current, pageSize } = getState().common.pageInfo
    dispatch(getHospitalSetAsync(current, pageSize)) // 再次请求分页数据 
  }
)

// 锁定状态 用前台做
/* export const getHospitalSetLockIdAsync = (id:number,status:number)=>(
  async (dispatch:any)=>{
    await getHospitalSetLockId(id,status)
    dispatch(changeStateById(id)) // 再次请求分页数据
  }
) */

// 添加医院设置 修改医院也是这个接口
export const postHospitalSetAsync = (body: any) => (
  async (dispatch: any, getState: any) => {
    console.log(body);
    if (body.id) {
      // 修改
      await putHospitalSet(body)
    } else {
      //添加
      await postHospitalSet(body)
    }
    const { current, pageSize } = getState().common.pageInfo;
    dispatch(getHospitalSetAsync(current, pageSize))
  }
)

// 根据id删除医院设置信息      
export const deleteHospitalSetByIdAsync = (id: number) => (
  async (dispatch: any, getState: any) => {
    await deleteHospitalSetById(id) // 完成删除
    const { current, pageSize } = getState().common.pageInfo;
    await dispatch(getHospitalSetAsync(current, pageSize)); // 更新现在的内容
  }
)


// 批量删除医院设置信息     
export const deleteBatchMoveAsync = (data: React.Key[]) => {
  return async (dispatch: any, getState: any) => {
    await deleteBatchMove(data);
    const { current, pageSize } = getState().common.pageInfo;
    await dispatch(getHospitalSetAsync(current, pageSize));
  }
}



//导出状态
export const useSelectorHosp = () => useSelector((state: any) => state.hosp)

//导出模块
export default hospSlice.reducer
